package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.FreeDelivery;
import com.dingreading.cloud.admin.service.FreeDeliveryService;
import com.dingreading.cloud.admin.service.GoodsService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/system/freeDelivery")
@Api(tags = "总部-包邮模板管理")
public class FreeDeliveryController {

    @Resource
    protected FreeDeliveryService freeDeliveryService;
    @Resource
    protected GoodsService goodsService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "关键字 模板名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "包邮类型 0.到付 1.包邮 2.满金额包邮 3.满数量包邮") @RequestParam(value = "type", required = false) Integer type
    ) {
        Page<FreeDelivery> pageList = freeDeliveryService.pageList(pageUtil, keyword, type);

        long totalRow = pageList.getTotalRow();
        List<FreeDelivery> records = pageList.getRecords();

        PageBeanDto data = new PageBeanDto();
        data.setTotal(totalRow);
        data.setList(records);
        return R.ok(data);
    }


    @ApiOperation(value = "添加/编辑")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "包邮模板id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "模板名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "包邮类型 0.到付 1.包邮 2.满金额包邮 3.满数量包邮") @RequestParam(value = "type") Integer type,
            @ApiParam(value = "包邮数量") @RequestParam(value = "freeNum", required = false) Integer freeNum,
            @ApiParam(value = "包邮金额") @RequestParam(value = "freeAmount", required = false) Double freeAmount
    ) {
        if (StringUtils.isBlank(name))
            return R.fail("请填写包邮模板名称");
        if (type == null)
            return R.fail("请选择包邮类型");
        if (type.equals(2)) {
            if (freeAmount == null || freeAmount <= 0)
                return R.fail("请填写正确的包邮金额");
        } else if (type.equals(3)) {
            if (freeNum == null || freeNum <= 0)
                return R.fail("请填写正确的包邮数量");
        }

        boolean b = freeDeliveryService.haveSameName(id, name);
        if (b)
            return R.fail("名称重复了");

        FreeDelivery data = null;
        if (id == null || id <= 0) {
            // 新增
            data = new FreeDelivery();
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
            data.setType(type);
            data.setTypeName(getTypeName(type));
            data.setEnabled(1);
        } else {
            // 修改
            data = freeDeliveryService.getById(id);
            if (data == null)
                return R.fail("获取失败");
        }
        data.setName(name);
        if (freeNum != null)
            data.setFreeNum(freeNum);
        if (freeAmount != null)
            data.setFreeAmount(freeAmount);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        freeDeliveryService.saveOrUpdate(data);

        return R.ok();
    }

    /**
     * 获取包邮类型的名称
     */
    private String getTypeName(Integer type) {
        if (type.equals(0))
            return "到付";
        else if (type.equals(1))
            return "包邮";
        else if (type.equals(2))
            return "满金额包邮";
        else if (type.equals(3))
            return "满数量包邮";
        return "";
    }


    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        FreeDelivery data = freeDeliveryService.getById(id);
        if (data == null)
            return R.fail("获取失败");

        return R.ok(data);
    }


    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        if (enabled.equals(0)) {
            boolean b = goodsService.haveFreeDeliveryId(id);
            if (b)
                return R.fail("已被使用，不能禁用");
        }

        boolean b = freeDeliveryService.updateEnabled(id, enabled);
        if (!b)
            return R.fail("更新失败");

        return R.ok();
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        boolean b = goodsService.haveFreeDeliveryId(id);
        if (b)
            return R.fail("已被使用，不能删除");

        boolean i = freeDeliveryService.removeById(id);
        if (!i)
            return R.failDel();

        return R.ok();
    }


    @ApiOperation(value = "获取可用的包邮模板")
    @PostMapping(value = "/getList", produces = "application/json; charset=UTF-8")
    public R<Object> getList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName
    ) {
        List<FreeDelivery> list = freeDeliveryService.listByEnabled();

        return R.ok(list);
    }

//    
//    @ApiOperation(value = "商品种类添加包邮模板")
//    @PostMapping(value = "/addFreeDelivery", produces = "application/json; charset=UTF-8")
//    public R<Object> addFreeDelivery(
//            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
//            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
//            @ApiParam(value = "商品种类id") @RequestParam(value = "goodCategoryId") Long goodCategoryId,
//            @ApiParam(value = "包邮模板id") @RequestParam(value = "freeDeliveryId") Long freeDeliveryId
//    ) {
//        
//
//        boolean b = goodsCategoryService.addFreeDelivery(goodCategoryId, freeDeliveryId);
//        if (!b)
//            return R.fail("添加失败");
//
//        return R.ok();
//    }

}
